<template>
  <div>
    <!-- 导航栏 -->
    <van-nav-bar
      title="编辑昵称"
      left-text="取消"
      right-text="确认"
      @click-left="$emit('close')"
      @click-right="onClickRight"
    />

    <!-- 输入框 -->
    <van-field
      v-model="message"
      rows="2"
      autosize
      type="textarea"
      maxlength="11"
      placeholder="请输入昵称"
      show-word-limit
      v-model.trim="message"
    />
  </div>
</template>

<script>
import { updateUserProfileAPI } from '../../../api/index.js'
export default {
  name: 'UsernameEdit',
  props: {
    name: {
      type: String,
      default: ''
    }
  },
  data () {
    return {
      message: this.name
    }
  },
  methods: {
    async onClickRight () {
      try {
        await updateUserProfileAPI({
          name: this.message
        })
        this.$toast.success('修改成功')

        // 修改成功后需要关闭页面并更新内容
        this.$emit('update:name', this.message)
        this.$emit('close')
      } catch (error) {
        console.log(error)
        this.$toast.fail('修改失败')
      }
    }
  }

}
</script>

<style>

</style>
